<?php
// Takes the average across arrays - array positions with same key values
function ee_avg_across_arrays($arr1, $arr2) {
	if(count($arr1) != count($arr2))
		die("Error in function add_across_arrays: the two arrays are not the same size");
	
	// add across the arrays
	$sum_array = array();
	foreach ($arr1 as $key => $val) {
		$sum_array[] = $val + $arr2[$key];
	}
	
	$avg_array = array();
	foreach($sum_array as $key => $val) {
		$avg_array[] = $val / 2;
	}
	
	return $avg_array;
}



// Function to calculate the mean of an array
function ee_mean($arr) {
	return array_sum($arr) / count($arr);
}

// Function to calculate square of value - mean
function ee_sd_square($x, $mean) { 
	return pow($x - $mean,2);
}

// Function to calculate standard deviation (uses sd_square)    
function ee_sd($array) {
    // square root of sum of squares devided by N-1
    $map = array_map("ee_sd_square", $array, array_fill(0,count($array), (array_sum($array) / count($array)) ) );
    return sqrt(array_sum($map) / (count($array) - 1) );
}

